/**
 * Choicesoft.com.cn Inc.
 * Copyright (c) 2004-2018 All Rights Reserved.
 */
package com.chen.ssh;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

/**
 * @author chenxubing
 * @version $Id: SshUtils.java, v 0.1 2018-12-05 1:15 PM chenxubing Exp $$
 */
public final class SshUtils {
    private SshUtils() {
        throw new AssertionError();
    }

    public static Session openSession(String sshHost, int sshPort, String sshUser, String sshPassword, String host, int port, int localPort) throws Exception{
        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(sshUser, sshHost, sshPort);
            session.setPassword(sshPassword);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();
            session.setPortForwardingL("localhost", localPort, host, port);
            return session;
        } catch (Exception e) {
            throw e;
        }
    }

    public static void closeSession(Session session) {
        if (null != session) {
            session.disconnect();
        }
    }
}
